﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Indigo.Quibbler.Common;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace Indigo.Quibbler.Controls
{
    /// <summary>
    /// Interaction logic for AdsControl.xaml
    /// </summary>
    public partial class ToolbarControl : UserControl
    {
        #region Private Members
        private ImageButton _lastSelectedImageButton = null;
        private ImageButton _currentSelectedImageButton = null;
        private bool _searchStreak = false;
        private bool _isShowingFontOptions = false;
        private SolidColorBrush _selectedFontBrush = new SolidColorBrush(Colors.Black);
        private SolidColorBrush _deSelectedFontBrush = new SolidColorBrush(Colors.Gray);
        
        //private SolidColorBrush _selectedFontBrush = new SolidColorBrush(Color.FromArgb(255, 224, 224, 224));
        //private SolidColorBrush _deSelectedFontBrush = new SolidColorBrush(Color.FromArgb(255, 140, 140, 140));
        private bool _isAutoHide = false;
        private DispatcherTimer _barHideTimer = new DispatcherTimer();
        #endregion

        #region Delegates
        public delegate void ZoomLevelChangedEventHandler(object sender, ZoomLevel zoomLevel);
        #endregion

        #region Public Events
        public event EventHandler HomeClick;
        public event EventHandler LiveShareClick;
        public event EventHandler SpiderClick;
        public event EventHandler CoverFlowClick;
        public event EventHandler TileClick;
        public event EventHandler Tile3DClick;
        public event EventHandler CommentClick;
        public event EventHandler FeedbackClick;
        public event SearchClickedEventHandler SearchClick;
        public event ZoomLevelChangedEventHandler ZoomLevelChanged;
        public event EventHandler StartScribble;
        public event EventHandler EndScribble;
        public event EventHandler StartReadOutLoud;
        public event EventHandler EndReadOutLoud;
        public event EventHandler HideBar;
        #endregion

        #region Constructor
        public ToolbarControl()
        {
            InitializeComponent();
            this.Loaded += _ToolbarControl_Loaded;
        }
        #endregion

        #region Public Delegates
        public delegate void SearchClickedEventHandler(object sender, string searchString);
        #endregion

        #region Handlers
        void _ToolbarControl_Loaded(object sender, RoutedEventArgs e)
        {
            //btnHome.IsEnabled = false;
            //btnShare.IsEnabled = false;
            //btnDelete.IsEnabled = false;
            //btnFeedback.IsEnabled = false;
            //btnScribble.IsEnabled = false;

            btnHome.Click += new EventHandler(_btnHome_Click);
            btnShare.Click +=new EventHandler(_btnShare_Click);
            btnCarousel.Click += new EventHandler(_btnCoverFlow_Click);
            btnTile.Click += new EventHandler(_btnTile_Click);
            btnTile3D.Click += new EventHandler(_btnTile3D_Click);
            btnSpider.Click += new EventHandler(_btnSpider_Click);
            btnSearch.Click += new EventHandler(_btnSearch_Click);
            txtSearch.GotFocus += _searchTextBoxFocus;
            txtSearch.LostFocus += _searchTextBoxLostFocus;
            txtSearch.KeyUp += new KeyEventHandler(txtSearch_KeyUp);

            btnZoom.Click += new EventHandler(btnZoom_Click);
            btnZoom.LostFocus += new RoutedEventHandler(_btnZoom_LostFocus);
            btnComment.Click += new EventHandler(btnComment_Click);
            btnReadAloud.Click += new EventHandler(btnReadAloud_Click);
            btnScribble.Click += new EventHandler(btnScribble_Click);
            btnFeedback.Click += new EventHandler(btnFeedback_Click);

            lblLarge.MouseDown += new MouseButtonEventHandler(lblLarge_MouseDown);
            lblMedium.MouseDown += new MouseButtonEventHandler(lblMedium_MouseDown);
            lblSmall.MouseDown += new MouseButtonEventHandler(lblSmall_MouseDown);

            _barHideTimer.Interval = TimeSpan.FromSeconds(5);
            _barHideTimer.Tick += new EventHandler(_barHideTimer_Tick);
            btnPinBar.Click += _btnPinBar_Click;

            this.MouseEnter += new MouseEventHandler(_ToolbarControl_MouseEnter);
            this.MouseLeave += new MouseEventHandler(_ToolbarControl_MouseLeave);

            _SelectFontOptions(lblMedium, ZoomLevel.Medium);
        }

      
        void _ToolbarControl_MouseEnter(object sender, MouseEventArgs e)
        {
            _barHideTimer.Stop();
        }
        void _ToolbarControl_MouseLeave(object sender, MouseEventArgs e)
        {
            if (_isAutoHide)
            {
                _barHideTimer.Start();
            }
        }


        #region Pin/Unpin Bar
        void _btnPinBar_Click(object sender, EventArgs e)
        {
            if (_isAutoHide) //if autohide is true //Unpinned presently
            {
                btnPinBar.SetValue(ToolTipService.ToolTipProperty, "Unpin");
                //Pin it
                _barHideTimer.Stop();
                System.Windows.Controls.Image imageControlAutoHide = (System.Windows.Controls.Image)VisualTreeHelper.GetChild(btnPinBar, 0);
                imageControlAutoHide.Source = new BitmapImage(new Uri("pack://application:,,/Quibbler - Universal News Reader;component/Images/IconAutoHideDisabled.png", UriKind.RelativeOrAbsolute));
            }
            else //UnPin 
            {
                btnPinBar.SetValue(ToolTipService.ToolTipProperty, "Pin");
                //Stop Timer
                System.Windows.Controls.Image imageControlAutoHide = (System.Windows.Controls.Image)VisualTreeHelper.GetChild(btnPinBar, 0);
                imageControlAutoHide.Source = new BitmapImage(new Uri("pack://application:,,/Quibbler - Universal News Reader;component/Images/IconAutoHideEnabled.png", UriKind.RelativeOrAbsolute));
                _barHideTimer.Stop();
                if (HideBar != null) HideBar(this, null);
            }
            _isAutoHide = !_isAutoHide;
        }
        void _barHideTimer_Tick(object sender, EventArgs e)
        {
            if (HideBar != null)
            {
                _barHideTimer.Stop();
                HideBar(this, null);
            }
        }

        #endregion

        #region Core Font Size Events
        void lblSmall_MouseDown(object sender, MouseButtonEventArgs e)
        {
            _SelectFontOptions(lblSmall, ZoomLevel.Low);
        }
        void lblMedium_MouseDown(object sender, MouseButtonEventArgs e)
        {
            _SelectFontOptions(lblMedium, ZoomLevel.Medium);
        }
        void lblLarge_MouseDown(object sender, MouseButtonEventArgs e)
        {
            _SelectFontOptions(lblLarge, ZoomLevel.High);
        }
        void _btnZoom_LostFocus(object sender, RoutedEventArgs e)
        {
            _HideFontOptions();
        }
        #endregion

        #region Core Article Gallery Features
        void btnScribble_Click(object sender, EventArgs e)
        {
            //if (_currentSelectedImageButton != null && _currentSelectedImageButton.Name.Equals("btnScribble"))
            //{
            //    _currentSelectedImageButton.ToggleStatus = false;
            //    _currentSelectedImageButton = null;
            //    return;
            //}
            _RaiseScribble();
        }

        void btnReadAloud_Click(object sender, EventArgs e)
        {
            _CancelPreviousEvent();
            if (_currentSelectedImageButton != null && _currentSelectedImageButton.Name.Equals("btnReadAloud"))
            {
                _currentSelectedImageButton.ToggleStatus = false;
                _currentSelectedImageButton = null;
                return;
            }
            _HandleButtonsToggling(btnReadAloud);
            _RaiseReadOutLoud();
        }

        void btnComment_Click(object sender, EventArgs e)
        {
            if (CommentClick != null)
                CommentClick(this, e);
        }

        void btnZoom_Click(object sender, EventArgs e)
        {
            //_CancelPreviousEvent();
            if (_isShowingFontOptions)
            {
                _HideFontOptions();
            }
            else
            {
                _isShowingFontOptions = true;
                (this.Resources["sbShowFontOptions"] as Storyboard).Begin();
                btnZoom.ToggleStatus = true;
            }
        }

        void btnFeedback_Click(object sender, EventArgs e) {
            if (FeedbackClick != null)
                FeedbackClick(this,e);
        }

        #endregion

        #region Core Search
        void txtSearch_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                _RaiseSearch();
        }

        void _btnSearch_Click(object sender, EventArgs e)
        {
            _HideFontOptions();
            _RaiseSearch();
        }

        private void _searchTextBoxFocus(object sender, RoutedEventArgs e)
        {
            _HideFontOptions();
            if (txtSearch.Text == "SEARCH")
                txtSearch.Text = string.Empty;
            txtSearch.SelectAll();
        }

        private void _searchTextBoxLostFocus(object sender, RoutedEventArgs e)
        {
            if (txtSearch.Text.Trim() == string.Empty)
                txtSearch.Text = "SEARCH";
        }
        #endregion

        #region Core Spider
        void _btnSpider_Click(object sender, EventArgs e)
        {
            _CancelPreviousEvent();
            _HideFontOptions();
            if (SpiderClick != null)
                SpiderClick(this, e);
        }

        #endregion

        #region Core Views
        void _btnTile3D_Click(object sender, EventArgs e)
        {
            _searchStreak = false;
            _HandleButtonsToggling(btnTile3D);
            if (Tile3DClick != null)
                Tile3DClick(this, e);
        }

        void _btnTile_Click(object sender, EventArgs e)
        {
            _searchStreak = false;
            _HandleButtonsToggling(btnTile);
            if (TileClick != null)
                TileClick(this, e);
        }

        void _btnCoverFlow_Click(object sender, EventArgs e)
        {
            _searchStreak = false;
            _HandleButtonsToggling(btnCarousel);
            if (CoverFlowClick != null)
                CoverFlowClick(this, e);
        }
        #endregion

        #region Misc
        void _btnHome_Click(object sender, EventArgs e)
        {
            if (HomeClick != null)
                HomeClick(this, e);
        }

        void _btnShare_Click(object sender, EventArgs e)
        {
            if (LiveShareClick != null)
                LiveShareClick(this, e);
        }

        #endregion

        #endregion

        #region Private Functions
        private void _SetArticleReadingMode(bool mode)
        {
            btnZoom.IsEnabled = mode;
            btnReadAloud.IsEnabled = mode;
            btnComment.IsEnabled = mode;
            btnScribble.IsEnabled = mode;
        }

        private void _RaiseSearch()
        {
            _CancelPreviousEvent();
            if (!txtSearch.Text.Trim().Equals("SEARCH"))
            {
                txtSearch.Text = txtSearch.Text.Trim();
                if (SearchClick != null)
                    SearchClick(this, txtSearch.Text.Trim());
            }
        }

        private void _CancelPreviousEvent()
        {
            if (_currentSelectedImageButton != null)
            {
                if (_currentSelectedImageButton.Name.Equals("btnScribble"))
                {
                    _CancelScribble();
                }
                else if (_currentSelectedImageButton.Name.Equals("btnReadAloud"))
                {
                    _CancelReadOutLoud();
                }
            }
        }

        private void _SelectFontOptions(Label lblSelected, ZoomLevel zoomLevel)
        {
            _ResetFontOptions();
            lblSelected.Foreground = _selectedFontBrush;
            lblSelected.FontSize += 3;
            _HideFontOptions();
            if (ZoomLevelChanged != null)
                ZoomLevelChanged(this, zoomLevel);
        }

        private void _HandleButtonsToggling(ImageButton nowPressed)
        {
            _HideFontOptions();
            if (_currentSelectedImageButton != null)
            {
                _currentSelectedImageButton.ToggleStatus = false;
                _lastSelectedImageButton = _currentSelectedImageButton;
            }
            _currentSelectedImageButton = nowPressed;
            _currentSelectedImageButton.ToggleStatus = true;
        }

        private void _HideFontOptions()
        {
            if (_isShowingFontOptions)
            {
                _isShowingFontOptions = false;
                (this.Resources["sbHideFontOptions"] as Storyboard).Begin();
                btnZoom.ToggleStatus = false;
            }
        }

        private void _ResetFontOptions()
        {
            lblSmall.Foreground = _deSelectedFontBrush;
            lblSmall.FontSize = 10;
            lblMedium.Foreground = _deSelectedFontBrush;
            lblMedium.FontSize = 11;
            lblLarge.Foreground = _deSelectedFontBrush;
            lblLarge.FontSize = 12;
        }


        private void _RaiseScribble()
        {
            if (StartScribble != null)
                StartScribble(this, null);
        }
        private void _CancelScribble()
        {
            if (EndScribble != null)
                EndScribble(this, null);
        }
        private void _RaiseReadOutLoud()
        {
            if (StartReadOutLoud != null)
                StartReadOutLoud(this, null);
        }
        private void _CancelReadOutLoud()
        {
            if (EndReadOutLoud != null)
                EndReadOutLoud(this, null);
        }

        #endregion

        #region Public Helper Functions
        public void SetSectionSettings(ViewType viewType)
        {
            btnCarousel.IsEnabled = true;
            btnTile.IsEnabled = true;
            btnTile3D.IsEnabled = true;
            btnSpider.IsEnabled = false;
            _SetArticleReadingMode(false);
            btnComment.IsEnabled = false;
            btnSearch.IsEnabled = true;
            txtSearch.IsEnabled = true;

            if (_currentSelectedImageButton != null)
            {
                _currentSelectedImageButton.ToggleStatus = false;
            }
            _lastSelectedImageButton = null;
            switch (viewType)
            {
                case ViewType.CoverFlow:
                    _currentSelectedImageButton = btnCarousel;
                    break;
                case ViewType.Tile:
                    _currentSelectedImageButton = btnTile;
                    break;
                case ViewType.Tile3D:
                    _currentSelectedImageButton = btnTile3D;
                    break;
                default:
                    break;
            }
            _currentSelectedImageButton.ToggleStatus = true;
            btnSearch.ToggleStatus = false;
            txtSearch.Text = "SEARCH";
        }

        public void SetArticlesListSettings(ViewType viewType)
        {
            //Carousel Enabled
            btnCarousel.IsEnabled = true;
            btnTile.IsEnabled = true;
            btnTile3D.IsEnabled = true;
            btnSpider.IsEnabled = false;
            _SetArticleReadingMode(false);
            btnSearch.IsEnabled = true;
            txtSearch.IsEnabled = true;

            if (_currentSelectedImageButton != null)
                _currentSelectedImageButton.ToggleStatus = false;
            _lastSelectedImageButton = null;
            switch (viewType)
            {
                case ViewType.CoverFlow:
                    //Carousel Enabled
                    _currentSelectedImageButton = btnCarousel;
                    break;
                case ViewType.Tile:
                    _currentSelectedImageButton = btnTile;
                    break;
                case ViewType.Tile3D:
                    _currentSelectedImageButton = btnTile3D;
                    break;
                default:
                    break;
            }
            _currentSelectedImageButton.ToggleStatus = true;
            btnSearch.ToggleStatus = false;
            txtSearch.Text = "SEARCH";
        }

        public void SetArticleGallerySettings()
        {
            btnCarousel.IsEnabled = false;
            btnTile.IsEnabled = false;
            btnTile3D.IsEnabled = false;
            btnSpider.IsEnabled = true;
            btnSpider.ToolTip = "Show Related Articles";
            _SetArticleReadingMode(true);
            //btnFeedback.ToolTip = "Post Comment";
            btnSearch.IsEnabled = true;
            txtSearch.IsEnabled = true;
            if (_currentSelectedImageButton != null)
                _currentSelectedImageButton.ToggleStatus = false;
            btnSearch.ToggleStatus = false;
            txtSearch.Text = "SEARCH";
        }

        public void SetPhotoGallerySettings()
        {
            btnCarousel.IsEnabled = false;
            btnTile.IsEnabled = false;
            btnTile3D.IsEnabled = false;
            btnSpider.IsEnabled = true;
            btnSpider.ToolTip = "Show Related Photos";
            _SetArticleReadingMode(false);
            btnSearch.IsEnabled = true;
            txtSearch.IsEnabled = true;
            if (_currentSelectedImageButton != null)
                _currentSelectedImageButton.ToggleStatus = false;
            btnSearch.ToggleStatus = false;
            txtSearch.Text = "SEARCH";
        }

        public void SetVideoGallerySettings()
        {
            btnCarousel.IsEnabled = true;
            btnTile.IsEnabled = true;
            btnTile3D.IsEnabled = true;
            btnSpider.IsEnabled = false;
            _SetArticleReadingMode(false);
            btnSearch.IsEnabled = true;
            txtSearch.IsEnabled = true;
            btnSearch.ToggleStatus = false;
            txtSearch.Text = "SEARCH";
        }

        public void SetSpiderSettings()
        {
            btnCarousel.IsEnabled = false;
            btnTile.IsEnabled = false;
            btnTile3D.IsEnabled = false;
            btnSpider.IsEnabled = true;
            _SetArticleReadingMode(false);
            btnSearch.IsEnabled = true;
            txtSearch.IsEnabled = true;
            if (_currentSelectedImageButton != null)
                _currentSelectedImageButton.ToggleStatus = false;
            btnSpider.ToggleStatus = true;
            _currentSelectedImageButton = btnSpider;
        }

        public void SetSearchSettings()
        {
            this.SetSpiderSettings();
            btnSearch.ToggleStatus = true;
        }

        public void SetDisableAllExceptSearchSettings()
        {
            btnCarousel.IsEnabled = false;
            btnTile.IsEnabled = false;
            btnTile3D.IsEnabled = false;
            btnSpider.IsEnabled = false;
            _SetArticleReadingMode(false);
            btnSearch.IsEnabled = true;
            txtSearch.IsEnabled = true;
            if (_currentSelectedImageButton != null)
                _currentSelectedImageButton.ToggleStatus = false;
            btnSearch.ToggleStatus = false;
            txtSearch.Text = "SEARCH";
        }

        public void ToggleReadAloudFalse()
        {
            if (_currentSelectedImageButton != null && _currentSelectedImageButton.Name.Equals("btnReadAloud"))
            {
                _currentSelectedImageButton.ToggleStatus = false;
                _currentSelectedImageButton = null;
            }
        }

        public void SetSpiderToolTip(string toolTipText)
        {
            btnSpider.ToolTip = toolTipText;
        }
        #endregion
    }
}

